package cst.a720yyq.zyfypt_720yyq.Adapter;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cst.a720yyq.zyfypt_720yyq.Bean.FocusResult;
import cst.a720yyq.zyfypt_720yyq.Bean.UserBean;
import cst.a720yyq.zyfypt_720yyq.FocusActivity;
import cst.a720yyq.zyfypt_720yyq.Interface.FocusListener;
import cst.a720yyq.zyfypt_720yyq.Modle.FocusModel;
import cst.a720yyq.zyfypt_720yyq.R;


public class FocusAdapter extends RecyclerView.Adapter  {
	private Context context;//上下文
	private LayoutInflater layoutInflater;//动态加载布局
	private List<FocusResult<UserBean>> list;//保存要显示的数据
	private SharedPreferences sp;
	private FocusListener focusListener;
	private int id;

	//1自定义：构造方法，传进上下文
	public FocusAdapter(Context context) {
		this.context=context;
		layoutInflater=LayoutInflater.from(context);
	}
	public void setList(List<FocusResult<UserBean>> list)
	{
		this.list=list;
	}
	//3自定义：ViewHolder子类
	public class ViewHolder extends RecyclerView.ViewHolder
	{   private ImageView imageView;
		private TextView username,realname,rolename;
		public ViewHolder(View itemView) {
			super(itemView);
			imageView=(ImageView)itemView.findViewById(R.id.focus_thumb);
			username=(TextView)itemView.findViewById(R.id.focus_id);
			realname=(TextView)itemView.findViewById(R.id.focus_name);
			rolename=(TextView)itemView.findViewById(R.id.focus_character);
		}
	}
	//4重写：生成Item的View
	@Override
	public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		View v=layoutInflater.inflate(R.layout.item_focus,parent,false);
		return new ViewHolder(v);
	}
	//5重写：给ViewHolder中的控件填充值，设置监听
	@Override
	public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
		focusListener=new FocusListener() {
			@Override
			public void onResponse(String msg) {

			}

			@Override
			public void onFail(String msg) {

			}
		};
		UserBean  userBean=list.get(position).getBean();
		if(userBean==null)
			return;
		final ViewHolder  viewHolder=(ViewHolder)holder;
		//viewHolder.imageView.setImageResource(articleBean.getImgid());
		viewHolder.username.setText(userBean.getUsername());
		viewHolder.realname.setText(userBean.getRealname());
		viewHolder.rolename.setText(userBean.getRolename());
		//异步加载图片
        /*Picasso.with(context)
                .load("http://amicool.neusoft.edu.cn/Uploads/"
                        +UserBean.getThumb())
                .placeholder(R.mipmap.ic_launcher)
                .into(viewHolder.imageView);*/
		viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener(){
			@Override
			public boolean onLongClick(View v) {
				int id=list.get(position).getId();

				return false;
			}
		});
		viewHolder.itemView.setTag(position);
		viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				//获取当前条目信息，如id
				int id = list.get(position).getId();
				Toast.makeText(context, "" + id, Toast.LENGTH_SHORT).show();
				//实际中，点击后打开新窗口做其他操作
				//Intent intent=new Intent(context, ViewArticleActivity.class);
				//intent.putExtra("resid",list.get(position).getId());
				//intent.putExtra("userid",list.get(position).getUserid());
				//context.startActivity(intent);
			}
		});
		sp=context.getSharedPreferences("ME",Context.MODE_PRIVATE);
		id=list.get(position).getBean().getId();
		final String sessionid=sp.getString("session_id",null);
		viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
				AlertDialog.Builder dialog = new AlertDialog.Builder(context);
				dialog.setTitle("取消关注");
				dialog.setMessage("确定吗？");
				dialog.setCancelable(false);
				dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						FocusModel focusModel = new FocusModel();
						focusModel.unfocus("userfocus", id, sessionid, focusListener);
						Intent intent=new Intent(context, FocusActivity.class);
						context.startActivity(intent);
						Activity activity=(Activity)context;
						activity.finish();
					}
				});
				dialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {

					}
				});
				dialog.show();
				return false;
			}
		});
	}
	//共有多少条目
	@Override
	public int getItemCount() {
		if(list==null)  return 0;
		else
			return list.size();
	}


}

